package com.example.administrator.energyida.util;

import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OceanUtil {
    private String url;
    private RequestBody requestBody;
    private HttpRequestCallback httpRequestCallback;
    private static OkHttpClient okHttpClient;

    public OceanUtil(String type, JSONObject jsonObject, HttpRequestCallback httpRequestCallback) {
        String domain = "http://47.101.187.18:8088/rjb/";
        this.url = domain + type;
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/x-www.form-urlencoded"), jsonObject.toString());
        this.requestBody = requestBody;
        this.httpRequestCallback = httpRequestCallback;
        okHttpClient = getOkHttpClient();
    }

    public void AsynPostInfo() {
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            if(response.isSuccessful())
            {
                String str = response.body().string();
                httpRequestCallback.callback(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void postInfo() {
        Request request = new Request.Builder().url(url).post(requestBody).build();
        okHttpClient.dispatcher().setMaxRequests(1);
        okHttpClient.dispatcher().setMaxRequestsPerHost(1);
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                httpRequestCallback.callback(str);
            }
        });
    }

    public void postInfo(final int time) {
        Request request = new Request.Builder().url(url).post(requestBody).build();
        okHttpClient.dispatcher().setMaxRequests(1);
        okHttpClient.dispatcher().setMaxRequestsPerHost(1);
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                httpRequestCallback.callback(str);
                try {
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                postInfo(time);
            }
        });
    }

    private synchronized OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient();
        }
        return okHttpClient;
    }


    public interface HttpRequestCallback {
        void callback(String strResponse);
    }

}
